package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        statement.setCreateTime(new Date());
        statement.setActualArrivalTime(new Date());
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        Statement old = this.get(statement.getId());
        if (!Statement.STATUS_CONSUME.equals(old.getStatus())) {
            throw new BusinessException("系统状态异常");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteRelation(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //判断结算单appointment_id 是否等于空, 如果不为空
            //需要把预约单状态修改成,到店状态.
            if (statementMapper.selectByPrimaryKey(dictId).getAppointmentId()!=null){
             appointmentMapper.updateStatusArrival(statementMapper.selectByPrimaryKey(dictId)
                     .getAppointmentId(),new Date(),1);
            }
            statementMapper.deleteByPrimaryKey(dictId);
            statementItemMapper.deleteByPrimaryKey(dictId);
        }

    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void updateConsume(Long statementId, BigDecimal totalPrice, BigDecimal totalQuantity, BigDecimal discountAmount) {
        statementMapper.updateConsume(statementId,totalPrice,totalQuantity,discountAmount);
    }

    @Override
    public void pay(Long statementId) {
        Statement statement = statementMapper.selectByPrimaryKey(statementId);
        if (!Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            throw new BusinessException("状态异常,请稍后重试");
        }
        //修改支付人,支付时间,支付状态
        statementMapper.changePayStatus(statementId,Statement.STATUS_PAID, ShiroUtils.getUser().getId());
    }

    @Override
    public Statement getByAppointmentId(Long id) {

        return statementMapper.selectByAppointmentId(id);
    }
}
